<?php
namespace dg\Controller;

use Think\Controller;

class RoleController extends ApibaseController {
    /*
     * 个人中心资料编辑
     * @date: 2015-3-23下午5:06:13
     * @editor: XING
     */
    public function userUpdate(){
    	if(I('post.update')){
    		$data = array('email'=>I('post.email'));
    		if(I('post.password')){
    			$salt = randstr(5);
    			$data['password'] = md5(I('post.password').$salt);
    			$data['salt'] = $salt;
    		}
    		$data['email'] = I('post.email');
    		session('kfLine'.I('post.rid'),I('post.lineStatus') == '1' ? '1' : null);
    		$keArr = session('kfLineArr');
    		if(I('post.lineStatus') == '1'){
    			if(isset($keArr[I('post.rid')]) && $keArr[I('post.rid')] = I('post.rid')){
    				// Nothing to do
    			}else{
    				$keArr[I('post.rid')] = I('post.rid');
    			}
    			session('kfLineArr',$keArr);
    		}else{
    			if(isset($keArr[I('post.rid')])){
    				unset($keArr[I('post.rid')]);
    				session('kfLineArr',$keArr);
    			}
    		}
    		M('ResEntity')->where(array('rid'=>session('loginUid')))->setField($data);
    		
    		$this->showtips('信息编辑成功');
    	}
    }

    /*
     * 后台角色管理
     * @date: 2016-7-21下午5:06:13
     * @editor: yanyan
     */
    public function index(){
        $role = M('Role')->select();
        $this->assign('getData',$role);
        $this->display('role/index');
    }

    /*
     * 后台角色添加
     * @date: 2016-7-21下午5:06:13
     * @editor: yanyan
     */
    public function add(){
        if(I('post.submit')){
            if(I('post.id')){
                $effectId = M('Role')->where(array('id'=>I('post.id')))
                    ->setField(array(
                        'name' => I('post.name'),
                        'remark' => I('post.remark'),
                        'status' => trim(I('post.status')),
                        'update_at' => time()
                    ));
                if($effectId){
                    $this->showtips('角色信息更新成功');
                }else{
                    $this->showtips('操作失败');
                }
            }
            $data = array(
                'name' => I('post.name'),
                'remark' => I('post.remark'),
                'status' => I('post.status'),
                'add_at' => time(),
                'update_at' => time()
            );
            if(M('Role')->add($data)){
                $this->showtips('角色添加成功');
            }else{
                $this->showtips('操作失败');
            }
        }
        if(I('get.id')){
            $roleData = M('Role')->where(array('id'=>I('get.id')))->find();
            $this->assign('roleData',$roleData);
        }
        $this->display('role/add');
    }

    public function assign_permission(){
        if(!is_login()){
            redirect('/user/login');exit;
        }
        //role_id
        $role_id = I('get.role_id');
        $getData = $this->get_child(0);
        if(I('get.role_id')){
            //显示角色已选择的权限
            $role_access = M('Access')->where(array('role_id'=>I('get.role_id')))->find();
            $node_arr = explode(',',$role_access['node_id']);
            $getData = $this->get_child(0,$node_arr);
            $this->assign('node_arr',$node_arr);
        }

        if(I('get.role_id') && I('get.nodes')){
            //查找之前是否有授权,有的话执行更改 没有执行添加
            $has_node = M('access')->where(array('role_id'=>I('get.role_id')))->find();
            //给角色授权
            if($has_node){
                $data = array(
                    'node_id' => substr(I('get.nodes'),0,-1),
                    'update_at'  => time(),
                );
                $res = M('access')->where(array('role_id' => I('get.role_id')))->save($data);
            }else{
                $res = M('access')->add(array(
                    'role_id' => I('get.role_id'),
                    'node_id' => substr(I('get.nodes'),0,-1),
                    'add_at'  => time(),
                    'update_at'  => time(),
                ));
            }
            echo $res?1:0;
        }
        $this->assign('role_id',$role_id);
        $this->assign('getData',$getData);
        $this->display('role/permission');
    }

    //获取相关子菜单
    public function get_child($id,$node_arr='') {
        $rows = M('Node')
            ->where('pid = ' . $id)->field('id,title,status,pid')->select();
        if ($rows) {
            foreach ($rows as $k => $v) {
                if($node_arr && in_array($v['id'],$node_arr)){
                    $rows[$k]['check'] = 1;
                }
                $rows[$k]['list'] = $this->get_child($v['id'],$node_arr);
            }
        }
        return $rows;
    }
}